//
//  WHRunConfigurationModel.m
//  WHPersonalData
//
//  Created by hyf on 2023/2/3.
//  Copyright © 2023 mac. All rights reserved.
//

#import "WHRunConfigurationModel.h"
//#import "WHUserRegiserModel.h"
#import "WHWproxyKitUtility.h"
#import "XMLDictionary.h"
//#import "WHSiteInfoModel.h"
#import "WHUseCertModel.h"
#import "NSDate+MJ.h"
//#import "WHTDSModel.h"
//#import "WHProxyInfoModel.h"
//#import "WHSpaceModel.h"
//#import "UserLoginModel.h"
#import "WHWriteLogTool.h"
#import "ParsingXMLUtility.h"

@implementation WHRunConfigurationModel

+(NSString *)getSystemEnvironment{
    NSString *appid = [[NSUserDefaults standardUserDefaults] objectForKey:mSystemEnvironment];
    return appid;
}

//保存系统环境
+(void)setSystemEnvironment:(NSString *)appid{
    NSLog(@"WHRunConfigurationModel --保存系统环境");
    [[NSUserDefaults standardUserDefaults] setObject:appid forKey:mSystemEnvironment];
}

//+(void)checkUserWithproxy:(NSString *)proxy target:(NSString *)target success:(void (^)(NSDictionary *infoDict))success failure:(void (^)(NSString *result))failure{
//    UserLoginModel * user = [UserLoginModel getLoginModel];
//    //参数
//    NSString *paramsStr = [NSString stringWithFormat:
//                           @"<c id=\"opertype\">qryuserinfo</c>"
//                           "<c id=\"code\">%@</c>"
//                      ,user.idcard];
//    
//    // 请求参数
//    NSString *soapStr = [NSString stringWithFormat:
//                         @"<request><oper>qryuserinfo</oper><random></random><info><cs>%@</cs></info></request>",paramsStr];
//    
//    WHWproxyKitUtility *whwProxyKit = [[WHWproxyKitUtility new] initWithProxyIP:proxy targetUri:target getParams:soapStr];
//    whwProxyKit.mParamsHaveEncoded = YES;
//    whwProxyKit.mUserType = @"0";
//    whwProxyKit.mHavePrompt = YES;
//    whwProxyKit.mPromptTxt = @"检查用户是否开户...";
//    [whwProxyKit getSyncActionWithprogress:^(NSString * _Nonnull result) {
//            
//    } success:^(NSString * _Nonnull result, NSString * _Nonnull xml, NSString * _Nonnull path) {
//        NSDictionary *dict = [NSDictionary dictionaryWithXMLString:xml];
//        NSString *code     = dict[REQUEST_RESP_CODE];
//        NSString *msg     = dict[REQUEST_RESP_MSG];
//        if ([code isEqualToString:REQUEST_RESP_EC00]){
//            NSDictionary *infoDict = dict[REQUEST_RESP_INFO];
//            success(infoDict);
//        }else{
//            failure(msg);
//        }
//    } failure:^(NSString * _Nonnull result) {
//        //failure(result);
//    }];
//    
//}
//
///*193.体验环境手机注册
// 方法名： phoneReg
// 入参：
// <request>
//     <oper>phoneReg</oper>
//     <random>客户端随机数，保留</random>
//     <info>
//        <cs>
//         <c id="opertype">phoneReg</c>
//         <c id="type">1-正式版 0-测试版</c>
//         <c id="cert">1-需要签发 0-不需要签发</c>
//         <c id="phone"></c>
//         <c id="qcode"></c>
//         <c id="publickey"></c>
//         <c id="siteid"></c>
//         <c id="psiteid"></c>
//         <c id="idcard"></c>
//         <c id="name"></c>
//         <c id="machinecode"></c>
//         <c id="model"></c>
//         <c id="brand"></c>
//         <c id="brandmodel"></c>
//         <c id="tokenid"></c>
//         <c id="ver"></c>
//        -- 以下信息是TDS产生，不需传入
//         <c id="regid"></c>
//         <cert>用户证书</cert>
//         <cacert>根证书</cacert>
//         
//       </cs>
//   </info>
//  
// </request>
// 返回结果
// <ret>
//      <code>EC00-成功,其他-失败</code>
//      <msg>若是失败，失败原因</msg>
//      <info>成功返回的结果</info>
//  <ret>
// info结构
//  <RESPONSE>
//       <info>
//         <ver>版本号</ver>
//         <username>用户姓名</username>
//         <siteid>交换箱ID</siteid>
//         <psiteurl>交换站地址</psiteurl>
//         <psitename>交换站地址</psitename>
//         <lan>交换站地址</lan>
//         <area>交换站地址</area>
//         <brand>交换站地址</brand>
//         <token>交换站地址</token>
//         <chat>
//           <id></id>
//           <name></name>
//           <siteid></siteid>
//           <psiteid></psiteid>
//           <psitename></psitename>
//           <host></host>
//           <lan></lan>
//           <area></area>
//           <proxyid></proxyid>
//           <proxyurl></proxyurl>
//         </chat>
//         
//         <tdsid>目录系统标识</tdsid>
//         <proxy>目录代理地址</proxy>
//           <cert>用户证书</cert>
//       </info>
//       <code>EC00</code>
//       <msg>处理成功</msg>
//     </RESPONSE>  */
//#pragma mark - 用户其他环境注册开始
//+(void)userRegiserWithModel:(WHUserRegiserModel*)userRegiserModel sysid:(NSString *)sysid proxy:(NSString *)proxy target:(NSString *)target success:(void (^)(void))success failure:(void (^)(NSString *result))failure{
//    WHSiteInfoModel *site = [WHSiteInfoModel getWHSiteInfoModelWithId:getUserPhone];
//    UserLoginModel * user = [UserLoginModel getLoginModel];
//    
//    //参数
//    NSString *paramsStr = [NSString stringWithFormat:
//                           @"<c id=\"opertype\">phoneReg</c>"
//                           "<c id=\"type\">0</c>"
//                           "<c id=\"cert\">1</c>"
//                           "<c id=\"phone\">%@</c>"
//                           "<c id=\"qcode\">%@</c>"
//                           "<c id=\"publickey\">%@</c>"
//                           "<c id=\"siteid\">%@</c>"
//                           "<c id=\"psiteid\">%@</c>"
//                           "<c id=\"idcard\">%@</c>"
//                           "<c id=\"name\">%@</c>"
//                           "<c id=\"machinecode\">%@</c>"
//                           "<c id=\"model\">2</c>"
//                           "<c id=\"brand\">apple</c>"
//                           "<c id=\"brandmodel\"></c>"
//                           "<c id=\"tokenid\">%@</c>"
//                           "<c id=\"ver\">%@</c>"
//                      ,userRegiserModel.phone,userRegiserModel.qcode,userRegiserModel.publicKey,site.siteid,site.psiteid,user.idcard,site.name,userRegiserModel.machinecode,site.token,site.ext3];
//    // 请求参数
//    NSString *soapStr = [NSString stringWithFormat:
//                         @"<request><oper>phoneReg</oper><random></random><info><cs>%@</cs></info></request>",paramsStr];
//
//    WHWproxyKitUtility *whwProxyKit = [[WHWproxyKitUtility new] initWithProxyIP:proxy targetUri:target getParams:soapStr];
//    whwProxyKit.mParamsHaveEncoded = YES;
//    whwProxyKit.mUserType = @"0";
//    whwProxyKit.mHavePrompt = NO;
//    whwProxyKit.mPromptTxt = @"用户注册...";
//    [whwProxyKit getSyncActionWithprogress:^(NSString * _Nonnull result) {
//        
//    } success:^(NSString * _Nonnull result,NSString * _Nonnull xml, NSString * _Nonnull path) {
//        [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--注册xml：%@",xml]];
//        NSString *  xmls   = xml;
//        NSDictionary *dict = [NSDictionary dictionaryWithXMLString:xmls];
//        NSString *code     = dict[REQUEST_RESP_CODE];
//        NSString *msg     = dict[REQUEST_RESP_MSG];
//        if ([code isEqualToString:REQUEST_RESP_EC00]){
//            NSDictionary *infoDict = dict[REQUEST_RESP_INFO];
//            NSDictionary *respDict = infoDict[REQUEST_RESP_RESPONSE];
//            NSString *code         = respDict[REQUEST_RESP_CODE];
//            NSDictionary *userDict = respDict[REQUEST_RESP_INFO];
//            if ([code isEqualToString:REQUEST_RESP_EC00]){
//                DebugLog(@"获取用户信息成功");
//                NSString *phone = userRegiserModel.phone;
//                NSString *qcode = userRegiserModel.qcode;
//                NSString *datatime = [NSDate currentDateHourTimeString];
//                NSString *cerVersion = @"0";
//                #pragma mark - 证书相关
//                NSString *pkey                  = userRegiserModel.privateKey; //本手机私钥
//                NSString *cert                  = userDict[WHUseCert_cert]; //本手机证书
//                NSString *ce06                  = userDict[WHUseCert_ce06]; //交换代理证书
//                NSString *ce07                  = userDict[WHUseCert_ce07]; //交换系统正书
//                NSString *cacert                = userDict[WHUseCert_cacert]; //根证书
//                WHUseCertModel *pkeyCertModel   = [WHUseCertModel new];
//                pkeyCertModel.certtyppe         = WHUseCert_uspkey;
//                pkeyCertModel.cert              = pkey;
//                pkeyCertModel.phone             = phone;
//                pkeyCertModel.datatime          = datatime;
//                pkeyCertModel.ext2              = cerVersion;
//                pkeyCertModel.ext3              = sysid;
//                pkeyCertModel.ext4              = qcode;
//                
//                WHUseCertModel *certCertModel   = [WHUseCertModel new];
//                certCertModel.certtyppe         = WHUseCert_uscert;
//                certCertModel.cert              = cert;
//                certCertModel.phone             = phone;
//                certCertModel.datatime          = datatime;
//                certCertModel.ext2              = cerVersion;
//                certCertModel.ext3              = sysid;
//                certCertModel.ext4              = qcode;
//                
//                WHUseCertModel *ce06CertModel   = [WHUseCertModel new];
//                ce06CertModel.certtyppe         = WHUseCert_ce06;
//                ce06CertModel.cert              = ce06;
//                ce06CertModel.phone             = phone;
//                ce06CertModel.datatime          = datatime;
//                ce06CertModel.ext2              = cerVersion;
//                ce06CertModel.ext3              = sysid;
//                ce06CertModel.ext4              = qcode;
//                   
//                WHUseCertModel *ce07CertModel   = [WHUseCertModel new];
//                ce07CertModel.certtyppe         = WHUseCert_ce07;
//                ce07CertModel.cert              = ce07;
//                ce07CertModel.phone             = phone;
//                ce07CertModel.datatime          = datatime;
//                ce07CertModel.ext2              = cerVersion;
//                ce07CertModel.ext3              = sysid;
//                ce07CertModel.ext4              = qcode;
//                   
//                WHUseCertModel *cacertCertModel = [WHUseCertModel new];
//                cacertCertModel.certtyppe       = WHUseCert_cacert;
//                cacertCertModel.cert            = cacert;
//                cacertCertModel.phone           = phone;
//                cacertCertModel.datatime        = datatime;
//                cacertCertModel.ext2            = cerVersion;
//                cacertCertModel.ext3            = sysid;
//                cacertCertModel.ext4            = qcode;
//                
//                NSArray *wHUseCertModels = @[pkeyCertModel,certCertModel,ce06CertModel,ce07CertModel,cacertCertModel];
//                
//                __block NSArray *spaceProxyArr;
//                //开启事务
//                [SqliteDBAccess executeSql:@"BEGIN IMMEDIATE TRANSACTION" toDatabase:nil succesefulBlock:^{
//                    __block bool re = YES;
//                    
//                    [WHUseCertModel delWHUserCertModel:phone sys:sysid succesefulBlock:^{
//                        
//                    } andFailureBlock:^(NSString * _Nonnull msg) {
//                        [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--删除证书表失败！错误信息：%@",msg]];
//                        re = NO;
//                    }];
//                    
//                    for(int i = 0;i <wHUseCertModels.count;i++){
//                        WHUseCertModel *wHUseCertModel = wHUseCertModels[i];
//                        [WHUseCertModel addUseComModel:wHUseCertModel succesefulBlock:^(WHUseCertModel * _Nonnull model) {
//                            DebugLog(@"添加证书表成功");
//                            [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--%@--添加证书表成功！",model.certtyppe]];
//                        } andFailureBlock:^(NSString * _Nonnull msg) {
//                            DebugLog(@"添加证书表失败");
//                            [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--添加证书表失败！错误信息：%@",msg]];
//                            re = NO;
//                        }];
//                    }
//                    
//                    #pragma mark - tds相关
//                    NSString *name        = @""; //平台名称
//                    NSString *uri         = userDict[@"tdsid"];  //平台标识
//                    //TDS信息
//                    WHTDSModel*tDSModel=[WHTDSModel new];
//                    tDSModel.phone   = phone;
//                    tDSModel.uri     = uri;
//                    tDSModel.name    = name;
//                    tDSModel.datatime= datatime;
//                    tDSModel.rquri   = sysid;
//                   
//                    [WHTDSModel addWHTDSModelModel:tDSModel succesefulBlock:^(NSString * _Nonnull result) {
//                        DebugLog(@"添加tds表成功");
//                        [WHWriteLogTool writeLogInfomation:@"WHRunConfigurationModel--添加tds表成功！"];
//                    } andFailureBlock:^(NSString * _Nonnull msg) {
//                        DebugLog(@"添加tds表失败");
//                        [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--添加tds表失败！错误信息：%@",msg]];
//                        re = NO;
//                    }];
//                    
//                    //TDS代理地址
//                    NSString * proxyurl = userDict[@"proxy"];
//                    WHProxyInfoModel *proxyInfoModel = [WHProxyInfoModel new];
//                    proxyInfoModel.phone             = phone;
//                    proxyInfoModel.uri               = uri;
//                    proxyInfoModel.name              = name;
//                    proxyInfoModel.proxyuri          = @"";
//                    proxyInfoModel.proxyurl          = proxyurl;
//                    proxyInfoModel.datatime          = datatime;
//                    proxyInfoModel.rquri             = sysid;
//                    //保存TDS代理地址
//                    [WHProxyInfoModel addWHProxyInfoModel:proxyInfoModel succesefulBlock:^(NSString * _Nonnull result) {
//                        DebugLog(@"添加代理表成功");
//                        [WHWriteLogTool writeLogInfomation:@"WHRunConfigurationModel--添加代理表成功！"];
//                    } andFailureBlock:^(NSString * _Nonnull msg) {
//                        DebugLog(@"添加代理表失败");
//                        [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--添加代理表失败！错误信息：%@",msg]];
//                        re = NO;
//                    }];
//                    
//                    #pragma mark - 空间信息相关
//                    NSDictionary*chatDic            = userDict[@"chat"];
//                    NSString *chatId                = chatDic[@"id"];
//                    NSString *chatName              = chatDic[@"name"];
//                    NSDictionary*proxysDict         = chatDic[@"proxys"]; //空间代理地址
//                    
//                    WHSpaceModel*spaceModel=[WHSpaceModel new];
//                    spaceModel.phone   = phone;
//                    spaceModel.uri     = chatId;
//                    spaceModel.name    = chatName;
//                    spaceModel.datatime= datatime;
//                    spaceModel.rquri   = sysid;
//                
//                    [WHSpaceModel addWHSpaceModel:spaceModel succesefulBlock:^(NSString * _Nonnull result) {
//                        DebugLog(@"添加空间表成功");
//                        [WHWriteLogTool writeLogInfomation:@"WHRunConfigurationModel--添加空间表成功！"];
//                    } andFailureBlock:^(NSString * _Nonnull msg) {
//                        DebugLog(@"添加空间表失败");
//                        [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--添加空间表失败！错误信息：%@",msg]];
//                        re = NO;
//                    }];
//                
//                    //空间多个代理地址
//                    if([proxysDict[@"proxy"] isKindOfClass:[NSDictionary class]]){
//                        spaceProxyArr =@[proxysDict[@"proxy"]];
//                    }else if ([proxysDict[@"proxy"] isKindOfClass:[NSArray class]]){
//                        spaceProxyArr =proxysDict[@"proxy"];
//                    }
//                    
//                    for(int i=0;i<spaceProxyArr.count;i++){
//                        NSDictionary*dict                = spaceProxyArr[i];
//                        NSString * proxyurl              = [NSString stringWithFormat:@"%@:%@",dict[@"_url"],dict[@"_port"]];
//                        WHProxyInfoModel *proxyInfoModel = [WHProxyInfoModel new];
//                        proxyInfoModel.phone             = phone;
//                        proxyInfoModel.uri               = chatId;
//                        proxyInfoModel.name              = chatName;
//                        proxyInfoModel.proxyuri          = @"";
//                        proxyInfoModel.proxyurl          = proxyurl;
//                        proxyInfoModel.datatime          = datatime;
//                        proxyInfoModel.rquri             = sysid;
//                        //保存空间代理地址
//                        [WHProxyInfoModel addWHProxyInfoModel:proxyInfoModel succesefulBlock:^(NSString * _Nonnull result) {
//                            DebugLog(@"添加空间代理表成功");
//                            [WHWriteLogTool writeLogInfomation:@"WHRunConfigurationModel--添加空间代理表成功！"];
//                        } andFailureBlock:^(NSString * _Nonnull msg) {
//                            DebugLog(@"添加空间代理表失败");
//                            [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--添加空间代理表失败！错误信息：%@",msg]];
//                            re = NO;
//                        }];
//                    }
//                    
//                    if(re){
//                        //提交事务
//                        [SqliteDBAccess executeSql:@"COMMIT TRANSACTION" toDatabase:nil succesefulBlock:^{
//                            DebugLog(@"提交事务成功！");
//                            [WHWriteLogTool writeLogInfomation:@"WHRunConfigurationModel--提交事务成功！"];
//                            NSDictionary*chatDic            = userDict[@"chat"];
//                            NSString *chatId                = chatDic[@"id"];
//                            NSDictionary*spaceProxyDic = spaceProxyArr[arc4random_uniform((int)spaceProxyArr.count)];
//                            NSString *chatProxy = [NSString stringWithFormat:@"%@:%@",spaceProxyDic[@"_url"],spaceProxyDic[@"_port"]];
//                            NSString *infoStr = [userDict innerXML];
//                            infoStr =[NSString stringWithFormat:@"<info>%@</info>",infoStr];
//                            #pragma mark - 注册成功以后通知空间
//                            NSString *tdsid = releaseAppId;
//                            if([sysid isEqualToString:mDevAppId]){
//                                tdsid = debugAppId;
//                            }else if ([sysid isEqualToString:mExperAppId]){
//                                tdsid = expAppId;
//                            }else if ([sysid isEqualToString:mTestAppId]){
//                                tdsid = testAppId;
//                            }
//                            NSString *tmpString = [ParsingXMLUtility delXMLNodeWithXML:infoStr NodeString:@"//info" stringNodes:@[@"cert",@"cacert",@"ce06",@"publickey",@"ce07"]];
//                            [WHUserRegiserModel sendUserRegiserInfoToSpaceWithIphone:phone withUserUri:qcode withProxy:chatProxy withTarget:chatId withSys:sysid withTdsid:tdsid withUserInfo:tmpString success:^(NSDictionary *result) {
//                                success();
//                            } failure:^(NSString *result) {
//                                [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--发送空间失败：%@！",result]];
//                                failure(result);
//                            }];
//                            
//                        } andFailureBlock:^(NSString *msg) {
//                            DebugLog(@"提交事务失败：%@", msg);
//                            [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--提交事务失败：%@！",msg]];
//                            failure(msg);
//                        }];
//                    }else{
//                        //回滚事务
//                        [SqliteDBAccess executeSql:@"ROLLBACK TRANSACTION" toDatabase:nil succesefulBlock:^{
//                            DebugLog(@"回滚成功！");
//                            [WHWriteLogTool writeLogInfomation:@"WHRunConfigurationModel--回滚成功,数据保存失败！"];
//                            failure(@"数据保存失败！");
//                        } andFailureBlock:^(NSString *msg){
//                            DebugLog(@"回滚失败：%@", msg);
//                            [WHWriteLogTool writeLogInfomation:[NSString stringWithFormat:@"WHRunConfigurationModel--回滚失败:%@,数据保存失败！",msg]];
//                            failure(msg);
//                        }];
//                    }
//                } andFailureBlock:^(NSString *msg) {
//                    DebugLog(@"事务启动失败 %@", msg);
//                    failure(@"数据保存失败！");
//                }];
//            }
//        }else{
//            failure(msg);
//        }
//    } failure:^(NSString * _Nonnull result) {
//        //failure(result);
//    }];
//}
#pragma mark - 用户其他环境注册结束

@end
